package com.sinothk.hussars.controller;


import com.sinothk.hussars.base.BaseController;
import com.sinothk.hussars.base.entity.PageReq;
import com.sinothk.hussars.entity.WorkerEditVo;
import com.sinothk.hussars.entity.WorkerManagerEntity;
import com.sinothk.hussars.entity.WorkerAddVo;
import com.sinothk.hussars.service.WorkerManagerService;
import com.sinothk.hussars.utils.XUtils;
import com.sinothk.server.support.annotation.callback.BizException;
import com.sinothk.server.support.annotation.callback.ResponseResult;
import com.sinothk.server.support.entity.PageData;
import com.sinothk.server.support.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 * 监管部门用户表 前端控制器
 * </p>
 *
 * @author liangyt
 * @since 2021-01-04
 */
@Api(tags = "机构_监管部门_人员")
@RestController
@RequestMapping("/workerManager")
@ResponseResult()
public class WorkerManagerController extends BaseController {

    private final WorkerManagerService workerManagerService;

    public WorkerManagerController(WorkerManagerService workerManagerService) {
        this.workerManagerService = workerManagerService;
    }

    @ApiOperation(value = "新增：监管人员")
    @PostMapping("/save")
    public WorkerManagerEntity save(@RequestBody WorkerAddVo workerManagerVo) {
        // 参数验证
        if (StringUtil.isEmpty(workerManagerVo.getAccount())) throw new BizException("账号不能为空");
        if (StringUtil.isEmpty(workerManagerVo.getName())) throw new BizException("姓名不能为空");

        return workerManagerService.add(workerManagerVo);
    }

    @ApiOperation(value = "查询：监管人员列表")
    @PostMapping("/usersPage")
    public PageData usersPage(@RequestBody PageReq<WorkerManagerEntity> pageVo) {
        // 参数验证
        if (pageVo.getPageSize() == 0) pageVo.setPageSize(20);
        return workerManagerService.usersPage(pageVo);
    }

    @ApiOperation(value = "更新：监管人员")
    @PostMapping("/update")
    public WorkerManagerEntity update(@RequestBody WorkerEditVo workerVo) {
        // 参数验证
        if (StringUtil.isEmpty(workerVo.getId())) throw new BizException("人员Id不能为空");

        WorkerManagerEntity worker = XUtils.classUtil().copyByMethod(workerVo, WorkerEditVo.class, WorkerManagerEntity.class);
        worker.setUpdateDate(new Date());
        workerManagerService.updateById(worker);
        return workerManagerService.getById(workerVo.getId());
    }
}

